home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-01 / snip1292.zip / DBLROUND.C < prev    next >
C/C++ Source or Header  |  1992-12-26  |  632b  |  35 lines

  1. /*
  2. **  DBLROUND.C - Rounds a double to the nearest whole number
  3. **  public domain by Ross Cottrell
  4. */
  5.  
  6. #include <float.h>
  7. #include <assert.h>
  8.  
  9. double round(double x)
  10. {
  11.       assert(1 == FLT_ROUNDS);
  12.       x += 1.0 / DBL_EPSILON;
  13.       return x - 1.0 / DBL_EPSILON;
  14. }
  15.  
  16. #ifdef TEST
  17.  
  18. #include <stdio.h>
  19. #include <stdlib.h>
  20.  
  21. void main(int argc, char *argv[])
  22. {
  23.       double val;
  24.       char *dummy;
  25.  
  26.       while (--argc)
  27.       {
  28.             val = strtod((const char *)(*(++argv)), &dummy);
  29.             printf("round(%g) = ", val);
  30.             printf("%.12g\n", round(val));
  31.       }
  32. }
  33.  
  34. #endif /* TEST */
  35.